package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUserPositionLevelHistory;
import com.ruoyi.system.service.ISysUserPositionLevelHistoryService;
import com.ruoyi.system.domain.SysUserPosition;
import com.ruoyi.system.mapper.SysUserPositionLevelHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserPositionLevelHistoryServiceImpl implements ISysUserPositionLevelHistoryService {
    @Autowired
    private SysUserPositionLevelHistoryMapper sysUserPositionLevelHistoryMapper;

    @Override
    public int insertUserPositionLevelByUserId(Long positionId, Long userId, Date nowDate) {
        return sysUserPositionLevelHistoryMapper.insertUserPositionLevelByUserId(positionId, userId, nowDate);
    }

    @Override
    public List<SysUserPositionLevelHistory> getPositionsListByNowDate(Date nowDate) {
        return sysUserPositionLevelHistoryMapper.getPositionsListByNowDate(DateUtils.parseDateToStr("yyyy-MM", nowDate));
    }

    @Override
    public SysUserPositionLevelHistory getPositionsByUserIdAndNowDate(Long userId, Date nowDate) {
        return sysUserPositionLevelHistoryMapper.getPositionsByUserIdAndNowDate(userId, DateUtils.parseDateToStr("yyyy-MM", nowDate));
    }

    @Override
    public int insertOrUpdateUserPositionHistoryByUserId(Long positionId, Long userId, Date nowDate) {
        String nowDateStr = DateUtils.parseDateToStr("yyyy-MM", nowDate);
        int num = 0;
        if (getPositionsByUserIdAndNowDate(userId, nowDate) == null) {
            //历史表没有当月数据时直接插入
            num = insertUserPositionLevelByUserId(positionId, userId, nowDate);
        } else {
            //有当月数据时更新
            num = sysUserPositionLevelHistoryMapper.updateUserPositionLevelHistoryByUserId(positionId, userId, nowDate, nowDateStr);
        }
        return 0;
    }
}
